package com.aaa.whj.controller;

import com.aaa.sys.entity.TabUser;
import com.aaa.whj.entity.Plan;
import com.aaa.whj.service.IPlanService;
import com.aaa.whj.service.impl.PlanServiceImpl;
import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.StringUtils;
import com.util.LayData;
import com.util.PrintJsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/plan")
public class PlanServlet extends HttpServlet {
    private final IPlanService iPlanService=new PlanServiceImpl();
    SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date(System.currentTimeMillis());
    String format = formatter.format(date);
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        if(!StringUtils.isNullOrEmpty(method)&&method.equals("findAll")){
            queryAll(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("del")){
            del(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("update")){
            updatePlan(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("addScl")){
            addPlan(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("query_scl")){
            query_plan(req,resp);
        }else if(!StringUtils.isNullOrEmpty(method)&&method.equals("only_one")){
            only_one(req,resp);
        }
    }

    private void only_one(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String code1 = req.getParameter("code1");
        System.out.println("code1 = " + code1);
        //List<Map<String, Object>> maps = iPlanService.only_one(code1);
        PrintWriter writer = resp.getWriter();
//        if(maps.size()==0){
//            writer.print("0");
//        }else {
//            writer.print("1");
//        }
        writer.print("0");
        writer.flush();
        writer.close();
    }

    private void query_plan(HttpServletRequest req, HttpServletResponse resp) {

    }

    private void addPlan(HttpServletRequest req, HttpServletResponse resp) {

        String plan_name = req.getParameter("plan_name");
        String plan_state = req.getParameter("plan_state");

        String plan_invite_code = req.getParameter("plan_invite_code");

        String startTime = req.getParameter("startTime");

        String endTime = req.getParameter("endTime");

        HttpSession session = req.getSession();
        TabUser user = (TabUser) session.getAttribute("user");
        System.out.println("user = " + user);
        Plan plan=new Plan(plan_name,plan_state,startTime,endTime,plan_invite_code,format);
        plan.setPlan_creator(user.getUsername());
        System.out.println("plan = " + plan);
        int i = iPlanService.addScl(plan);

        LayData success=null;
        if(i>0){
            success = LayData.success(plan);
        }else {
            success = LayData.error();
        }
        PrintJsonData.printData(resp,success);
    }

    private void updatePlan(HttpServletRequest req, HttpServletResponse resp) {
        String plan_id = req.getParameter("plan_id");

        String plan_name = req.getParameter("plan_name");
        String plan_state = req.getParameter("plan_state");

        String plan_invite_code = req.getParameter("plan_invite_code");

        String startTime = req.getParameter("startTime");

        String endTime = req.getParameter("endTime");

        Plan plan=new Plan(plan_name,plan_id,plan_invite_code,plan_state,startTime,endTime,format);
        int i = iPlanService.updateScl(plan);
        LayData success=null;
        if(i>0){
            success = LayData.success(plan);
        }else {
            success = LayData.error();
        }
        PrintJsonData.printData(resp,success);
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) {
        String plan_id=req.getParameter("plan_id");
        int i = iPlanService.deleteById(Integer.valueOf(plan_id));

    }

    private void queryAll(HttpServletRequest req, HttpServletResponse resp) {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        List<Plan> plans = iPlanService.queryAll(page, limit);
        //System.out.println("scl = " + scl);
        long count = iPlanService.count(null);
        //System.out.println("count = " + count);
        LayData success = LayData.success((int) count, plans);
        PrintJsonData.printData(resp,success);

    }
}
